//
//  ReadQuestionViewController.m
//  YHYRealx
//
//  Created by yanghuiyan on 16/4/25.
//  Copyright © 2016年 yanghuiyan. All rights reserved.
//

#import "ReadQuestionViewController.h"
#import "DetailQuestionDataModel.h"
#import "ReadQuestionTVC.h"
#import "ReadAnswerTVC.h"
@interface ReadQuestionViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) NSMutableArray *dataSource;

@property (nonatomic, retain) DetailQuestionDataModel *questionModel;

@property (nonatomic, retain) YHYBaseTableView *tabelView;

@end

@implementation ReadQuestionViewController

- (void)dealloc {
    
    [_dataSource release];
    [_tabelView release];
    [_questionModel release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.dataSource = [NSMutableArray array];
    [self creatView];
    [self dataHandle];
}

#pragma mark - 数据解析
- (void) dataHandle {
    
    NSString *string = [NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/question/%@", self.idNew];
    NSLog(@"%@", string);
    [YHYAFNetTool GETNetWirhUrl:string body:nil headerFile:nil response:YHYJSON Success:^(id result) {
        self.questionModel = [[DetailQuestionDataModel alloc] initWithDataSource:result[@"data"]];
        NSLog(@"%@", self.questionModel.answer_title);
        
        [self.tabelView reloadData];
    } Failure:^(NSError *error) {
        NSLog(@"failure");
    }];
}

#pragma mark - 创建视图
- (void)creatView {
    
    self.view.backgroundColor = [UIColor colorWithRed:247 / 255.0 green:248 / 255.0 blue:249 / 255.0 alpha:1];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    titleLabel.text = @"问题";
    titleLabel.font = [UIFont boldSystemFontOfSize:22];
    titleLabel.textColor = [UIColor grayColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    self.navigationItem.titleView = titleLabel;
    [titleLabel release];
    
    self.tabelView = [[YHYBaseTableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.tabelView.delegate = self;
    self.tabelView.dataSource = self;
    [self.tabelView registerClass:[ReadQuestionTVC class] forCellReuseIdentifier:@"question"];
    [self.tabelView registerClass:[ReadAnswerTVC class] forCellReuseIdentifier:@"Answer"];
    [self.view addSubview:self.tabelView];
    
    [_tabelView release];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 1) {
        
        ReadAnswerTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"Answer"];
        cell.questionModel = self.questionModel;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    ReadQuestionTVC *cell = [tableView dequeueReusableCellWithIdentifier:@"question"];
    cell.questionModel = self.questionModel;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        if (indexPath.row == 1) {
            return HEIGHT * 0.75 + 20;
        }
        DetailQuestionDataModel *model = self.questionModel;
        
        CGFloat Height = [ReadQuestionTVC heightWithTitle:model.question_title];
        CGFloat height = [ReadQuestionTVC textWithHeight:model.question_content];
        return height+ Height + 40;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
